package com.server.util;

import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.ToString;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
 * @author
 */
@ToString
@AllArgsConstructor
@Accessors(chain = true)
@JsonInclude(JsonInclude.Include.ALWAYS)
public class Result<T> implements Serializable {

    @ApiModelProperty(value = "状态码")
    private int code;

    @ApiModelProperty(value = "描述")
    private String msg;

    @ApiModelProperty(value = "数据")
    private T data;

    @ApiModelProperty(value = "分页信息")
    private PageInfo page;

    public PageInfo getPage() {
        return page;
    }

    public void setPage(PageInfo page) {
        this.page = page;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }


    public Result(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public static <T> Result<T> error(String msg) {

        Result<T> result = new Result<>();
        result.setCode(HttpStatus.HTTP_UNAVAILABLE);
        result.setMsg(msg);
        return result;
    }

    public static <T> Result<T> error(int error, String msg) {

        Result<T> result = new Result<>();
        result.setCode(error);
        result.setMsg(msg);
        return result;
    }


    public Result() {
        this.code = HttpStatus.HTTP_OK;
        this.msg = "操作成功";
    }

    public Result(T t) {
        this.code = HttpStatus.HTTP_OK;
        this.msg = "操作成功";
        this.data = t;
    }

    public static <T> Result<T> ok(String msg) {
        return new Result<T>(HttpStatus.HTTP_OK, msg);
    }

    public static <T> Result<T> ok() {
        return new Result<T>(HttpStatus.HTTP_OK, "操作成功");
    }

    public static <T> Result<T> returnData(T data) {

        Result<T> result = new Result<>();
        result.setData(data);
        return result;
    }

    public static <T> Result<T> returnData(int code,T data) {

        Result<T> result = new Result<>();
        result.setData(data);
        result.setCode(code);
        return result;
    }

    public static <T> Result<T> createResult(IPage page) {
        Result result = new Result<>();
        PageInfo pageInfo = new PageInfo();
        pageInfo.setTotalCount(page.getTotal());
        pageInfo.setCurPage(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setPages(page.getPages());
        result.setData(page.getRecords());
        result.setPage(pageInfo);
        return result;
    }
}
